package com.alexander.annotation_api;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * author : cjw
 * date : 2022/2/9
 * description :
 */
public class RouterManager {
    private static final String TAG = "RouterManager";

    private static Map<String, String> pathRouter = new HashMap<>();
    private static RouterManager routerManager;

    static {
        initPathClassMap(pathRouter);
    }

    private static void initPathClassMap(Map<String, String> router) {
    }

    public static RouterManager getInstance() {
        if (routerManager == null) {
            synchronized (RouterManager.class) {
                if (routerManager == null) {
                    routerManager = new RouterManager();
                    return routerManager;
                }
            }
        }
        return routerManager;
    }

    public void navigation(Context context, String path) {
        String clazz = pathRouter.get(path);
        if (!TextUtils.isEmpty(clazz)) {
            jump(context, clazz);
        } else {
            Log.e(TAG, "class not found by path");
        }
    }

    private void jump(Context context, String className) {
        try {
            Class<?> clazz = Class.forName(className);
            Intent intent = new Intent(context, clazz);
            context.startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
